package com.apk.tools;

import java_util.coo_util.Log.C_Log;
import java_util.web.C_WebRequest;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class C_ProgressReport {
    public static final int o_unzip = 1;
    public static final int o_fixData = 2;
    public static final int o_app2build = 3;
    public static final int o_app2Sign = 4;
    public static final int o_upload = 5;
    public static final int o_uploadEnd = 6;
    public static int o_total;
    public static int o_currentProgress;
    public static final String o_url = "http://47.105.84.206:9221/gm/clipackage/packageResp.jhtml";

    public static void S_SendProgress(build_config setting, int Progress) throws UnsupportedEncodingException {
        S_SendProgress(setting, Progress, "");
    }

    public static void S_SendProgress(build_config setting, int Progress, String dnsUrl) throws UnsupportedEncodingException {
        o_currentProgress++;
        String msg = null;
        switch (setting.temp_buildType) {
            case 1:
                msg = "Win平台";
                break;
            case 2:
                msg = "Android";
                break;
            case 3:
                msg = "IOS";
                break;
        }

        switch (Progress) {
            case o_unzip:
                msg += "  正在解压";
                break;
            case o_fixData:
                msg += "  正在处理数据";
                break;
            case o_app2build:
                msg += "  正在压缩";
                break;
            case o_app2Sign:
                msg += "  正在签名";
                break;
            case o_upload:
                msg += "  正在上传";
                break;
            case o_uploadEnd:
                msg += "  上传完成";
                break;
        }
//        if (o_currentProgress == o_total) {
//            C_Log.error("超级错误....进度不能等于总进度......" + o_currentProgress);
//        }
        String str = "id=" + setting.temp_buildId
                + "&type=" + setting.temp_buildType
                + "&userId=" + java.net.URLEncoder.encode(setting.temp_userId, "UTF-8")
                + "&package=" + java.net.URLEncoder.encode(setting.temp_package, "UTF-8")
                + "&currentProgress=" + Progress
                + "&total=" + o_uploadEnd
                + "&msg=" + java.net.URLEncoder.encode(msg, "UTF-8")
                + "&dnsUrl=" + java.net.URLEncoder.encode(dnsUrl, "UTF-8");
        String urlData = o_url + "?" + str;
        C_WebRequest.S_Get(urlData);
        o_currentProgress++;

        //id type userId  package  currentProgress  total msg dnsUrl
    }


    final static String sh_appCopyToPC = "/run_appCopyToPC.sh";

    public static void S_upload(build_config setting, String appPath) throws IOException {
        C_ProgressReport.S_SendProgress(setting, C_ProgressReport.o_upload, "");
        File file = new File(setting.temp_wordPath + sh_appCopyToPC);
        String appCopy = file.getCanonicalPath();
        C_IpaUtil.S_fileCopy(sh_appCopyToPC, appCopy);
        C_cmd.S_Runtime("chmod 777 " + appCopy);
        if (setting.temp_buildType == 1) {

        } else {
            String appFile = "";
            if (setting.temp_buildType == 2) {
                appFile = setting.temp_userId + ".apk";
            } else if (setting.temp_buildType == 3) {
                appFile = setting.temp_userId + ".ipa";
            }
            C_cmd.S_Runtime(appCopy + " " + appFile + " " + appPath);
            String loadPath = "D:/VMware/macShareD/appUploadDNS/" + appFile;
            String urlData = "http://103.19.232.185:8017/package/uploadDns?"
                    + "id=" + setting.temp_buildId
                    + "&type=" + setting.temp_buildType
                    + "&userId=" + java.net.URLEncoder.encode(setting.temp_userId, "UTF-8")
                    + "&package=" + java.net.URLEncoder.encode(setting.temp_package, "UTF-8")
                    + "&loadPath=" + loadPath;

            C_WebRequest.S_Get(urlData);
        }


    }

    public static void S_uploadDNS(Map<String, String[]> map) throws IOException {
        build_config setting = new build_config();
        setting.temp_buildId = map.get("id")[0];
        setting.temp_buildType = Integer.parseInt(map.get("type")[0]);
        setting.temp_userId = map.get("userId")[0];
        setting.temp_package = map.get("package")[0];

        String loadPath = map.get("loadPath")[0];
        File appPath = new File(loadPath);
        String[] sss = appPath.getName().split("[.]");
        String userId = sss[0];
        //ossutil64 --config-file C:/ossutil64/ossutilconfig cp -r D:/VMware/macShareD/test2.bat oss://new-v3a8/autoclient/test/test2.bat -f
//        C_ProgressReport.S_SendProgress(setting, C_ProgressReport.o_upload, "");


        String path0 = userId + "/" + appPath.getName();
        S_uploadDNS(setting, loadPath, path0);
    }

    public static void S_uploadDNS(build_config setting, String loadPath, String path0) throws IOException {
        C_cmd.S_Runtime("ossutil64 --config-file C:/ossutil64/ossutilconfig cp -r " + loadPath + " oss://new-v3a8/autoclient/" + path0 + " -f");


        String dnsUrl = "https://tj.down.zifanweb.com/autoclient/" + path0;
        S_SendProgress(setting, o_uploadEnd, dnsUrl);

        C_Log.Log("上传完成......" + dnsUrl);
    }

    public static void S_SendEnd() {
        //  C_WebRequest.S_Get(o_url + "?type=" + 0 + "&" + o_total + "&total=" + o_total + "&msg=" + o_apk);
    }
}
